ホームに戻る
出典 :
関連 :
目次 :
WPFアプリケーションでPDFを表示する
WPFにはPDFを直接表示できる標準のコントロールは存在しないため、Windows.Data.Pdf を用いて画像に変換して表示を行う。
準備 : ランタイムAPIの有効化
以下の手順でMicrosoft.Windows.SDK.Contracts のインストールを行う。
(.NET 5.0以降を使用する場合は不要。)
-
Visual Studioのメニューにて「ツール」>「NuGet パッケージ マネージャー」>「パッケージ マネージャー設定」を選択する。
-
「オプション」ダイアログが開くので、「NuGet パッケージ マネージャー」>「全般」を選択し、
「既定のパッケージ管理方式」を「PackageReference」に変更し、「OK」を押す。
-
Visual Studioのメニューにて「プロジェクト」>「NuGet パッケージの管理」を選択する。
-
「参照」タブの検索欄に「Microsoft.Windows.SDK.Contracts」と入力し、
表示された「Microsoft.Windows.SDK.Contracts」を選択、「インストール」を押す。
アプリケーションの作成例
画面表示
メニュー「File」>「Open」を選択するとPDFファイル(G:\マイドライブ\8.Sagawa\test.pdf)が開く。
「Next」で次のページ、「Prev」で前のページに移動する。

XAMLの記述 (MainWindow.xaml)
<Window x:Class="ELP03_UI_Proto_ZZ.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ELP03_UI_Proto_ZZ"
mc:Ignorable="d"
Title="ELP03 UI Proto ZZ" Height="450" Width="800">
<Grid>
<StackPanel>
<Menu>
<MenuItem Header="File">
<MenuItem Header="Open" Name="menuOpen" Click="menuOpen_Click" /> <!-- PDFを開く -->
</MenuItem>
<MenuItem Header="Prev" Name="menuPrev" Click="menuPrev_Click" /> <!-- ページ戻し -->
<MenuItem Header="Next" Name="menuNext" Click="menuNext_Click" /> <!-- ページ送り -->
<TextBlock Name="txtPage" Text="Page" /> <!-- 現在のページ数 -->
</Menu>
<Image Name="imgMain" /> <!-- 画像化したPDF -->
</StackPanel>
</Grid>
</Window>
コードビハインドの記述 (MainWindow.xaml.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Windows.Data.Pdf;
namespace ELP03_UI_Proto_ZZ
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
/// - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = -
/// <summary>
/// コンストラクタ
/// </summary>
/// - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = -
public MainWindow()
{
InitializeComponent();
}
private Windows.Data.Pdf.PdfDocument pdfDocument;
private List<BitmapSource> pdfPages = new List<BitmapSource>();
private int DisplayPageNumber = 1;
/// - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = -
/// イベントハンドラ
/// - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = -
/// - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = -
/// <summary>
/// メニュー「Open」クリック時の処理
/// </summary>
/// - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = -
private async void menuOpen_Click(object sender, RoutedEventArgs e)
{
DisplayPageNumber = 1;
await ReadPDFtoImage();
ShowPage(DisplayPageNumber);
txtPage.Text = "Page " + DisplayPageNumber.ToString();
}
/// - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = -
/// <summary>
/// メニュー「Next」クリック時の処理
/// </summary>
/// - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = -
private void menuNext_Click(object sender, RoutedEventArgs e)
{
// 既に末尾ページまで到達している場合は何もしない
if (DisplayPageNumber >= pdfPages.Count)
{
return;
}
// ひとつ先のページを表示
DisplayPageNumber++;
ShowPage(DisplayPageNumber);
txtPage.Text = "Page " + DisplayPageNumber.ToString();
}
/// - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = -
/// <summary>
/// メニュー「Prev」クリック時の処理
/// </summary>
/// - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = -
private void menuPrev_Click(object sender, RoutedEventArgs e)
{
// 既に先頭ページまで到達している場合は何もしない
if (DisplayPageNumber <= 1)
{
return;
}
// ひとつ前のページを表示
DisplayPageNumber--;
ShowPage(DisplayPageNumber);
txtPage.Text = "Page " + DisplayPageNumber.ToString();
}
/// - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = -
/// private メソッド
/// - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = -
/// - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = -
/// <summary>
/// PDFファイルの読み込み
/// </summary>
/// - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = -
private async Task ReadPDFtoImage()
{
var file =
await Windows.Storage.StorageFile.GetFileFromPathAsync(
System.IO.Path.GetFullPath(@"G:\マイドライブ\8.Sagawa\test.pdf"));
try
{
// ファイルからPDFドキュメントを読み込む
pdfDocument = await Windows.Data.Pdf.PdfDocument.LoadFromFileAsync(file);
}
catch
{
}
// 読み込み成功時
if (pdfDocument != null)
{
// 各ページをビットマップ(BitmapSource)に変換する
for (uint i = 0; i < pdfDocument.PageCount; i++)
{
using Windows.Data.Pdf.PdfPage page = pdfDocument.GetPage(i);
using var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream();
// 解像度変更 96dpi ⇒ 200dpi
PdfPageRenderOptions renderOptions = new PdfPageRenderOptions();
renderOptions.DestinationWidth =
(uint)Math.Round(page.Dimensions.ArtBox.Width / 96.0 * 200.0);
await page.RenderToStreamAsync(stream, renderOptions);
PngBitmapDecoder decoder =
new PngBitmapDecoder(System.IO.WindowsRuntimeStreamExtensions.AsStream(stream),
BitmapCreateOptions.None,
BitmapCacheOption.OnLoad);
BitmapSource d = (BitmapSource)decoder.Frames[0];
pdfPages.Add(d);
}
}
}
/// - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = -
/// <summary>
/// 指定されたページを表示する
/// </summary>
/// <param name="p">ページ番号(1 ~)</param>
/// - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = -
private void ShowPage(int p)
{
if (pdfPages.Count == 0 || p < 1 || p > pdfPages.Count)
{
return;
}
imgMain.Source = pdfPages[p - 1];
}
}
}